import { returnDataObj } from "@/utils/objectUtil";
export const multiSelectDialogComposition = (
  fetchApi: any,
  listVo: any,
  timeTypeList: any = [],
  startPlaceholderText: string = "",
  endPlaceholderText: string = "",
  timeTypeVal: string = ""
) => {
  const list: any = ref([{}]),
    listLoading = ref(false),
    totalNum = ref(0),
    listQuery: any = ref({}),
    checkItemList:any=ref({}),
    timeType = ref(timeTypeVal),
    searchBoxHeight = ref(0),
    dateValue = ref([]),
    startPlaceholder = ref(startPlaceholderText),
    endPlaceholder = ref(endPlaceholderText);
  // 初始化搜索条件
  const setInitData = (initData: any) => {
    listQuery.value = Object.assign({}, initData.queryData);
    checkItemList.value = [...initData.checkItemList]
  };
  // 时间类型选择
  const timeTypeHandler = (val: string) => {
    dateValue.value = [];
    timeTypeList.map((time: any) => {
      listQuery.value[time.value + "Start"] = "";
      listQuery.value[time.value + "End"] = "";
      if (time.value === val) {
        timeType.value = val;
        startPlaceholder.value = time.placeholder + "开始时间";
        endPlaceholder.value = time.placeholder + "结束时间";
      }
    });
  };
  // 多条时间选择
  const dateChangeHandler = (val: any) => {
    if (!val || val == null || val === "") {
      timeTypeList.map((time: any) => {
        listQuery.value[time.value + "Start"] = "";
        listQuery.value[time.value + "End"] = "";
      });
      return;
    }
    timeTypeList.map((time: any) => {
      if (time.value === timeType.value) {
        listQuery.value[time.value + "Start"] = val[0] + " 00:00:00";
        listQuery.value[time.value + "End"] = val[1] + " 23:59:59";
      }
    });
  };
  // 页面条数选择
  const pageSizeChange = (val: number) => {
    listQuery.value.pageSize = val;
    fetchData("refresh");
  };
  //   页面跳转
  const pageCurrentChange = (val: number) => {
    listQuery.value.pageNumber = val;
    fetchData("");
  };
  //   搜索
  const handleFilter = () => {
    fetchData("refresh");
  };
  // 数据列表
  const fetchData = (type: string) => {
    if (type === "refresh") listQuery.value.pageNumber = 1;
    const _params = returnDataObj(listQuery.value);
    listLoading.value = true;
    fetchApi(_params)
      .then((backdata: any) => {
        listLoading.value = false;
        list.value = listVo(backdata.data.list,checkItemList.value);
        totalNum.value = backdata.data.page.totalCount;
      })
      .catch(() => {
        listLoading.value = false;
      });
  };

  return {
    listQuery,
    listLoading,
    list,
    totalNum,
    timeType,
    startPlaceholder,
    endPlaceholder,
    searchBoxHeight,
    dateValue,
    setInitData,
    pageSizeChange,
    pageCurrentChange,
    fetchData,
    handleFilter,
    timeTypeHandler,
    dateChangeHandler
  };
};
export default multiSelectDialogComposition;
